﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Metro;

namespace Logistics
{
    public partial class frmUserButtonPermissions : Form
    {
        ListViewColumnSorter lvwColumnSorter = new ListViewColumnSorter();
        Manager.tc_Permission objPermissions = new Manager.tc_Permission();
        public delegate void ModelEventHandler(Model.tc_User model);
        public ModelEventHandler OnModelEvent;
        private Model.tc_User User;
        public frmUserButtonPermissions()
        {
            InitializeComponent();
            this.listInfo.ListViewItemSorter = lvwColumnSorter;
            ImageList imgList = new ImageList();
            imgList.ImageSize = new Size(1, 20);//分别是宽和高
            listInfo.SmallImageList = imgList;
        }

        public frmUserButtonPermissions(Model.tc_User _user) : this()
        {
            User = _user;
        }

        private void AddListviewItem(Model.UserUserButtonModel model)
        {
            ListViewItem item = null;
            if (this.listInfo.Items.Count > 0)
            {
                item = MyForms.FindItemWithTag(listInfo, model.Id.ToString());
            }
            if (item == null)
            {
                item = new ListViewItem();
                item.Checked = model.IsGranted;
                item.Tag = model.Id.ToString();
                item.SubItems.Add(model.Name);
                this.listInfo.Items.Add(item);
            }
        }
        public void DataBind()
        {
            this.listInfo.Items.Clear();
            this.listInfo.BeginUpdate();
            var pList = objPermissions.GetList(User.Id, Model.EPermissionType.Buttons);
            var list = new List<Model.UserUserButtonModel>();
            var p = pList.FirstOrDefault(m => m.Name == ButtonPermissions.SAPInport.ToString());
            list.Add(new Model.UserUserButtonModel { Id = ButtonPermissions.SAPInport.ToString(), IsGranted = p != null ? p.IsGranted : false, Name = "SAP导入" });

            p = pList.FirstOrDefault(m => m.Name == ButtonPermissions.DataView.ToString());
            list.Add(new Model.UserUserButtonModel { Id = ButtonPermissions.DataView.ToString(), IsGranted = p != null ? p.IsGranted : false, Name = "数据查看" });

            p = pList.FirstOrDefault(m => m.Name == ButtonPermissions.Export.ToString());
            list.Add(new Model.UserUserButtonModel { Id = ButtonPermissions.Export.ToString(), IsGranted = p != null ? p.IsGranted : false, Name = "数据导出" });

            p = pList.FirstOrDefault(m => m.Name == ButtonPermissions.PermissionsAdministartor.ToString());
            list.Add(new Model.UserUserButtonModel { Id = ButtonPermissions.PermissionsAdministartor.ToString(), IsGranted = p != null ? p.IsGranted : false, Name = "权限管理" });
            foreach (var item in list)
            {
                AddListviewItem(item);
            }
            this.listInfo.EndUpdate();
        }
        private void listInfo_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (e.Column == lvwColumnSorter.SortColumn)
            {
                if (lvwColumnSorter.Order == SortOrder.Ascending)
                {
                    lvwColumnSorter.Order = SortOrder.Descending;
                }
                else
                {
                    lvwColumnSorter.Order = SortOrder.Ascending;
                }
            }
            else
            {
                lvwColumnSorter.SortColumn = e.Column;
                lvwColumnSorter.Order = SortOrder.Ascending;
            }
            this.listInfo.Sort();
        }

        private void frmUserButtonPermissions_Load(object sender, EventArgs e)
        {
            DataBind();
        }

        private void butSave_Click(object sender, EventArgs e)
        {
            var msg = "确定要保存按钮权限吗？";
            if (MessageBox.Show(msg, "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                foreach (ListViewItem item in listInfo.Items)
                {
                    var model = new Model.tc_Permission();
                    model.Id = Guid.NewGuid();
                    model.Name = item.Tag.ToString();
                    model.IsGranted = item.Checked;
                    model.Type = Model.EPermissionType.Buttons;
                    model.UserId = User?.Id;

                    var p = objPermissions.GetModel(model.UserId.ToString(), model.Name);
                    if (p == null)
                    {
                        objPermissions.Add(model);
                    }
                    else
                    {
                        if (p.IsGranted != model.IsGranted)
                            p.IsGranted = model.IsGranted;
                        objPermissions.Update(p);
                    }
                }
                OnModelEvent?.Invoke(User);
                MessageBox.Show("数据保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }

        private void butCancel_Click(object sender, EventArgs e)
        {
            var msg = "数据尚未保存，确认要取消吗？";
            if (MessageBox.Show(msg, "警告", MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                this.Close();
            }
        }
    }
}
